package com.jiyun.h2303b.service.impl;

import com.jiyun.h2303b.feign.AccountFeignService;
import com.jiyun.h2303b.feign.OrderFeignService;
import com.jiyun.h2303b.feign.StorageFeignService;
import com.jiyun.h2303b.pojo.TbBrand;
import com.jiyun.h2303b.service.BrandService;
import com.jiyun.h2303b.service.BusinessService;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class BusinessServiceImpl implements BusinessService {

    @Autowired
    private AccountFeignService accountFeignService;

    @Autowired
    private OrderFeignService orderFeignService;

    @Autowired
    private StorageFeignService storageFeignService;

    @Autowired
    private BrandService brandService;

    @GlobalTransactional
    @Override
    public void buy(int userId, String commodity_code, int count) {
        // 创建订单
        orderFeignService.create(userId, commodity_code, count);
        // 扣减库存
        storageFeignService.deduct(commodity_code,count);

        // 扣减金额

        accountFeignService.debit(userId,count*1);

//        int i  =1/0;

    }

    @Transactional
    @Override
    public void changeBrand() {
        TbBrand tbBrand = brandService.getById(4);
        tbBrand.setName("11111");
        brandService.updateById(tbBrand);
        // 有事务注解
        brandService.changeBrand();


    }
}
